﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietPremier.Areas.AdminCP.Models;
using MvcLibrary.Repository;
using System.IO;

namespace VietPremier.Areas.AdminCP.Controllers
{
    public class AdminSocialNetWorkController : Controller
    {
        #region "Index"
        public ActionResult Index()
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                SocialNetWorkModel model = new SocialNetWorkModel();

                foreach (var social in db.EntitySocialNetWorks)
                {
                    model.listSocialNetWork.Add(new SocialNetWork()
                    {
                        Id = social.Id,
                        Link = social.Link,
                        NetWork = social.NetWork
                    });
                }
                return View(model);
            }
        }
        #endregion //index

        #region "Edit"
        public ActionResult Edit(int idSocial)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                SocialNetWork model = new SocialNetWork();
                EntitySocialNetWork social = db.EntitySocialNetWorks.FirstOrDefault(o => o.Id == idSocial);
                if (social != null)
                {
                    model.Id = social.Id;
                    model.Link = social.Link;
                    model.NetWork = social.NetWork;
                    return PartialView(model);
                }
                return PartialView();
            }

        }

        [HttpPost]
        public JsonResult GetDetail(int idSocial)
        {
            SocialNetWork model = new SocialNetWork();
            model.Id = -1;

            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntitySocialNetWork social = db.EntitySocialNetWorks.FirstOrDefault(o => o.Id == idSocial);

                if (social != null)
                {
                    model.Id = social.Id;
                    model.Link = social.Link;
                    model.NetWork = social.NetWork;
                }
                return Json(model);
            }

        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(int idSocial, SocialNetWork model)
        {

            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                //check Id 
                EntitySocialNetWork social = db.EntitySocialNetWorks.Where(p => p.Id == idSocial).FirstOrDefault();
                social.Link = model.Link;
              
                db.SubmitChanges();
                return RedirectToAction("Index", "AdminSocialNetWork");
            }
        }

        #endregion //edit

    }
}
